package MainBoard;

import Model.Buttons;
import Model.Chess;
import Dialog.EndingDialog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class ChessBoard extends JPanel implements MouseListener {
    public static final int GAME_OVER = 0;
    public static final int GAME_START = 1;
    public static final int COLS = 19;
    public static final int ROWS = 19;
    //    棋盘图片
    private Image chessBoardImage;
    //    棋子图片
    private Image blackChessImage;
    private Image whiteChessImage;
    //    标记可否点击
    protected boolean clickable;
    //    棋子的坐标
    protected int chessX;
    protected int chessY;
    public int[][] chessCoord = new int[COLS][ROWS];
    //    用户着棋的坐标和次数
    protected int stepCount;
    protected int[][] step;
    //    设置游戏结束对话框
    protected EndingDialog endingDialog;

    /**
     * ChessBoard类的构造函数
     *
     * <p>在ChessBoard类的的构造函数中会对棋盘的样式初始化</p>
     */
    public ChessBoard() {
        initImages();
        initChessCoord();
        addMouseListener(this);
    }

    /**
     * 初始化棋盘中所用到的突破
     *
     * <p>包括棋盘图片，黑棋和白棋的图片</p>
     */
    public void initImages() {
        chessBoardImage = Toolkit.getDefaultToolkit().getImage(ChessBoard.class.getClassLoader().getResource("images/gobangBoard.png"));
        blackChessImage = Toolkit.getDefaultToolkit().getImage(ChessBoard.class.getClassLoader().getResource("images/blackChess.png"));
        whiteChessImage = Toolkit.getDefaultToolkit().getImage(ChessBoard.class.getClassLoader().getResource("images/whiteChess.png"));
    }

    /**
     * 初始化棋盘棋子的坐标
     *
     * <p>将棋盘中棋盘棋子坐标都初始化为0
     * 即初始状态下没有棋子<p/>
     */
    public void initChessCoord() {
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) {
                chessCoord[i][j] = Chess.noChess;
            }
        }
    }

    /**
     * 初始化用户着棋记录
     */
    public void initStep() {
        step = new int[1000][2];
        stepCount = 0;
    }

    /**
     * 保存用户着棋坐标
     *
     * @param posX 着棋的X坐标
     * @param posY 着棋的Y坐标
     */
    public void saveStep(int posX, int posY) {
        stepCount++;
        step[stepCount][0] = posX;
        step[stepCount][1] = posY;
    }

    /**
     * 用户悔棋
     */
    public void backStep() {
        if (stepCount >= 2) {
            chessCoord[step[stepCount][0]][step[stepCount][1]] = 0;
            chessCoord[step[stepCount - 1][0]][step[stepCount - 1][1]] = 0;
            stepCount = stepCount - 2;
        }
    }

    /**
     * 设置开始游戏的点击事件
     *
     * @param clickable 棋盘是否可以点击
     */
    public void setClickable(boolean clickable) {
        this.clickable = clickable;
    }

    /**
     * 对棋盘和棋子进行绘制
     *
     * @param graphics 绘图类的对象，用于绘图
     */
    @Override
    public void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);
        graphics.drawImage(chessBoardImage, 0, 0, null);
        for (int i = 0; i < ROWS; i++) {
            for (int j = 0; j < COLS; j++) {
                if (chessCoord[i][j] == Chess.black) {
                    graphics.drawImage(blackChessImage, 20 + i * 30, 20 + j * 30, null);
                } else if (chessCoord[i][j] == Chess.white) {
                    graphics.drawImage(whiteChessImage, 20 + i * 30, 20 + j * 30, null);
                }
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
